//
//  Extension+UIImage.swift
//  weibo
//
//  Created by 张城 on 16/7/28.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit


extension UIImage{
    
    
    static func getScreenShot()->UIImage{
        
        // 得到window
        let window = UIApplication.sharedApplication().keyWindow!
        
        //开启上下文
        UIGraphicsBeginImageContext((window.bounds.size))
        
        // 渲染到上下文
        window.drawViewHierarchyInRect((window.bounds), afterScreenUpdates: false)
        
        //从上下文获取image
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return image
    }
    
    
    func getImageScale(width: CGFloat) -> UIImage {
        if self.size.width < width {
            return self
        }
        
        let height = (width * self.size.height)/self.size.width
        
        let rect = CGRectMake(0, 0, width, height)
        
        UIGraphicsBeginImageContext(rect.size)
        
        self.drawInRect(rect)
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return result
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}